package tree;

/**
 * @author wcj
 * @Date 2021/7/7 0:35
 * @Version 1.0
 */
public class SameTree {

    /**
     * 判断两棵二叉树是否相同
     * 递归：
     * ①递归方法的设计：sameTree(TreeNode r1,TreeNode r2)
     * ②递归出口：r1 == null && r2 == null ,截止；r1 == null || r2 == null，截止；r1.val!=r2.val，截止
     * ③递归体：前序遍历
     *
     * @param r1
     * @param r2
     * @return
     */
    public static boolean sameTree(TreeNode r1, TreeNode r2) {
        if (r1 == null && r2 == null) {
            return true;
        }
        if (r1 == null || r2 == null) {
            return false;
        }
        if (r1.val != r2.val) {
            return false;
        }
        return sameTree(r1.left, r2.left) && sameTree(r1.right, r2.right);
    }
}
